<?php
$weekStartDay = 'Sunday';
$numOfWorkDaysPerWeek = 7;

$startDate = getWeekStartDate($viewDate, $weekStartDay);
$endDate = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate) + 7, date("Y", $startDate));

$weekDates = array();
for ($i = 0; $i < 7; $i++) {
   $newDate = mktime(0, 0, 0, date("m", $startDate), date("d", $startDate) + $i, date("Y", $startDate));
   $weekDates[] = array('date' => $newDate);
}

function getWeekStartDate($viewDate, $weekStartDay) {
   $notFound = true;
   $precedingDate = $viewDate;
   while ($notFound) {
      $strDay = date("l", $precedingDate);
      if ($strDay == $weekStartDay) {
         $notFound = false;
         break;
      }
      $precedingDate = mktime(0, 0, 0, date("m", $precedingDate), date("d", $precedingDate) - 1, date("Y", $precedingDate));
   }
   return $precedingDate;
}
?>

<table>
   <!-- display the headers for each day of the work week -->
   <tr>
      <th rowspan="2">Employees</th>
      <!-- short day name (ex: Mon) -->
      <th colspan="2"><?php echo(date("l", $weekDates[0]['date'])); ?></th>
      <th colspan="2"><?php echo(date("l", $weekDates[1]['date'])); ?></th>
      <th colspan="2"><?php echo(date("l", $weekDates[2]['date'])); ?></th>
      <th colspan="2"><?php echo(date("l", $weekDates[3]['date'])); ?></th>
      <th colspan="2"><?php echo(date("l", $weekDates[4]['date'])); ?></th>
      <th colspan="2"><?php echo(date("l", $weekDates[5]['date'])); ?></th>
      <th colspan="2"><?php echo(date("l", $weekDates[6]['date'])); ?></th>
   </tr>
   <tr>
      <!-- short month + date (ex: Mar 3) -->
      <th colspan="2"><?php echo(date("M\nd", $weekDates[0]['date'])); ?></th>
      <th colspan="2"><?php echo(date("M\nd", $weekDates[1]['date'])); ?></th>
      <th colspan="2"><?php echo(date("M\nd", $weekDates[2]['date'])); ?></th>
      <th colspan="2"><?php echo(date("M\nd", $weekDates[3]['date'])); ?></th>
      <th colspan="2"><?php echo(date("M\nd", $weekDates[4]['date'])); ?></th>
      <th colspan="2"><?php echo(date("M\nd", $weekDates[5]['date'])); ?></th>
      <th colspan="2"><?php echo(date("M\nd", $weekDates[6]['date'])); ?></th>
   </tr>

   <?php
   $checkRangeScheduleResult = checkRangeSchedule($_SESSION['userName'], $_SESSION['userPassHash'], date("Y-m-d", $startDate), date("Y-m-d", $endDate));
   $employees = $checkRangeScheduleResult['employees'];

   //grab the accounts
   $getAccountsResult = getAccounts($_SESSION['userName'], $_SESSION['userPassHash']);
   $accounts = $getAccountsResult['results'];

   //grab the roles
   $getRolesResult = getRoles($_SESSION['userName'], $_SESSION['userPassHash']);
   $roles = $getRolesResult[1];


//parse the result
   foreach ($employees as $employee) {
      $shifts = $employee['shifts'];
      ?>

      <tr>
         <td rowspan="3"><?php echo($employee['accName']); ?></td>
         <?php
         //parse the shifts

         if ($shifts != NULL) {
            for ($i = 0; $i < $numOfWorkDaysPerWeek; $i++) {
               $hasShift = false;
               foreach ($shifts as $shift) {
                  $shiftDate = $shift['datestamp'];
                  $shiftDate = strtotime($shiftDate);
                  $dayInterval = date("d", $shiftDate) - date("d", $startDate);
                  if ($dayInterval == $i) {
                     ?>
                     <td><?php echo($shift['startTime']); ?></td>
                     <td><?php echo($shift['roleName']); ?></td>
                     <?php
                  } else {
                     ?>
                     <td>&nbsp;</td>
                     <?php
                  }
               }
            }
            ?>
         </tr>
         <tr>
            <?php
            //parse the shifts
            for ($i = 0; $i < $numOfWorkDaysPerWeek; $i++) {
               $hasShift = false;
               foreach ($shifts as $shift) {
                  $shiftDate = $shift['datestamp'];
                  $shiftDate = strtotime($shiftDate);
                  $dayInterval = date("d", $shiftDate) - date("d", $startDate);
                  if ($dayInterval == $i) {
                     ?>
                     <td><?php echo($shift['endTime']); ?></td>
                     <td><?php echo($shift['shiftStatus']); ?></td>
                     <?php
                  } else {
                     ?>
                     <td>&nbsp;</td>
                     <?php
                  }
               }
            }
            ?>
         </tr>
         <tr>
            <?php
            //parse the shifts
            for ($i = 0; $i < $numOfWorkDaysPerWeek; $i++) {
               $hasShift = false;
               foreach ($shifts as $shift) {
                  $shiftDate = $shift['datestamp'];
                  $shiftDate = strtotime($shiftDate);
                  $dayInterval = date("d", $shiftDate) - date("d", $startDate);
                  if ($dayInterval == $i) {
                     ?>
                     <td colspan="2">
                        <a href="#mod<?php echo($shift['roleID'] . $shift['dateID'] . $employee['empID']); ?>" 
                           role="button" class="btn" data-toggle="modal">Modify</a>

                        <!-- Modal -->
                        <div id="mod<?php echo($shift['roleID'] . $shift['dateID'] . $employee['empID']); ?>" 
                             class="modal hide fade" role="dialog" 
                             aria-labelledby="myModalLabel" aria-hidden="true">

                           <div class="modal-header">
                              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                              <h5><?php echo(date("l, M d, Y", strtotime($shift['datestamp']))); ?></h5>
                              <h5><?php echo($employee['accName']); ?></h5>
                              <h5><?php echo($shift['roleName']); ?></h5>
                           </div>

                           <div class="modal-body">
                              <table>

                                 <tr>
                                 <form action="" method="post">
                                    <td>
                                       <input type="hidden" name="roleID" value="<?php echo($shift['roleID']); ?>">
                                       <input type="hidden" name="dateID" value="<?php echo($shift['dateID']); ?>">
                                       <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                                       <input type="hidden" name="fieldToModify" value="dateID">
                                       <label>Shift Date</label> 
                                    </td>
                                    <td>
                                       <input type="date" name="newValue" value="<?php echo($shift['datestamp']); ?>" />
                                    </td>
                                    <td>
                                       <input type="submit" name="action" value="Save" />
                                    </td>
                                 </form>
                                 </tr>

                                 <tr>
                                 <form action="" method="post">
                                    <td>
                                       <input type="hidden" name="roleID" value="<?php echo($shift['roleID']); ?>">
                                       <input type="hidden" name="dateID" value="<?php echo($shift['dateID']); ?>">
                                       <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                                       <input type="hidden" name="fieldToModify" value="empID">
                                       <label>Employee</label> 
                                    </td>
                                    <td>
                                       <select name="newValue">
                                          <?php
                                          foreach ($accounts as $account) {
                                             echo ("<option value=" . $account['accName']);
                                             if ($account['accName'] == $employee['accName']) {
                                                echo(" SELECTED ");
                                             }
                                             echo (">" . $account['accName'] . "</option>");
                                          }
                                          ?>
                                       </select>
                                    </td>
                                    <td>
                                       <input type="submit" name="action" value="Save" />
                                    </td>
                                 </form>
                                 </tr>

                                 <tr>
                                 <form action="" method="post">
                                    <td>
                                       <input type="hidden" name="roleID" value="<?php echo($shift['roleID']); ?>">
                                       <input type="hidden" name="dateID" value="<?php echo($shift['dateID']); ?>">
                                       <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                                       <input type="hidden" name="fieldToModify" value="roleID">
                                       <label>Role</label> 
                                    </td>
                                    <td>
                                       <select name="newValue">
                                          <?php
                                          foreach ($roles as $role) {
                                             echo ("<option value=" . $role['roleID']);
                                             if ($role['roleID'] == $shift['roleID']) {
                                                echo(" SELECTED ");
                                             }
                                             echo (">" . $role['roleName'] . "</option>");
                                          }
                                          ?>
                                       </select>
                                    </td>
                                    <td>
                                       <input type="submit" name="action" value="Save" />
                                    </td>
                                 </form>
                                 </tr>

                                 <tr>
                                 <form action="" method="post">
                                    <td>
                                       <input type="hidden" name="roleID" value="<?php echo($shift['roleID']); ?>">
                                       <input type="hidden" name="dateID" value="<?php echo($shift['dateID']); ?>">
                                       <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                                       <input type="hidden" name="fieldToModify" value="startTime">
                                       <label>Start Time</label> 
                                    </td>
                                    <td>
                                       <input type="time" name="newValue" value="<?php echo($shift['startTime']); ?>" />
                                    </td>
                                    <td>
                                       <input type="submit" name="action" value="Save" />
                                    </td>
                                 </form>
                                 </tr>

                                 <tr>
                                 <form action="" method="post">
                                    <td>
                                       <input type="hidden" name="roleID" value="<?php echo($shift['roleID']); ?>">
                                       <input type="hidden" name="dateID" value="<?php echo($shift['dateID']); ?>">
                                       <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                                       <input type="hidden" name="fieldToModify" value="endTime">
                                       <label>End Time</label> 
                                    </td>
                                    <td>
                                       <input type="time" name="newValue" value="<?php echo($shift['endTime']); ?>" />
                                    </td>
                                    <td>
                                       <input type="submit" name="action" value="Save" />
                                    </td>
                                 </form>
                                 </tr>

                                 <tr>
                                 <form action="" method="post">
                                    <td colspan="3">
                                       <input type="hidden" name="roleID" value="<?php echo($shift['roleID']); ?>">
                                       <input type="hidden" name="dateID" value="<?php echo($shift['dateID']); ?>">
                                       <input type="hidden" name="empID" value="<?php echo($employee['empID']); ?>">
                                       <input type="submit" name="action" value="Delete Shift" />
                                    </td>
                                 </form>
                                 </tr>

                              </table>
                           </div>

                           <div class="modal-footer">
                              <button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
                           </div>

                        </div>
                        <!-- Modal -->
                     </td>

                     <?php
                  } else {
                     ?>
                     <td>&nbsp;</td>
                     <?php
                  }
               }
            }
            ?>
         </tr>
         <?php
      } else {
         ?>
         <td colspan="7">&nbsp;</td>
      </tr>
      <tr><td colspan="7">&nbsp;</td></tr>
      <tr><td colspan="7">&nbsp;</td></tr>
      <?php
   }
}
?>

</table>
